package org.edu.modules.oa.controller;


import com.aliyun.oss.OSS;
import groovy.util.logging.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.RedisUtil;
import org.edu.modules.oa.entity.VeOaYinzhangEntity;
import org.edu.modules.oa.entity.VeOaYinzhangLogEntity;
import org.edu.modules.oa.service.IVeOaYinzhangLogService;
import org.edu.modules.oa.vo.VeOaYinzhangLogVo;
import org.edu.modules.oa.vo.VeOaYinzhangVo;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Api(tags="印章管理-公共印章使用记录")
@RestController
@RequestMapping("/oa/VeOaCommonYinzhangLog")
@Slf4j
public class VeOaCommonYinzhangLogController {


    @Autowired
    private RedisUtil redisUtil; // redis服务类
    @Resource
    private OSS ossClient;
    @Resource
    private IVeOaYinzhangLogService veOaYinzhangLogService;


    //查询，显示表格 (用户名拼接的字符串)
    @AutoLog(value = "公共印章使用记录-显示表格")
    @ApiOperation(value = "查看公章使用记录表格", notes = "公共印章使用记录-显示表格")
    @PostMapping(value = "/searchTable")
    public Result<?> searchTable(Integer currentPage, Integer pageSize, VeOaYinzhangLogEntity veOaYinzhangLogEntity
    ,@RequestParam List<String> nameList){
        veOaYinzhangLogEntity.setTypeStr("公共印章");
        HashMap<String,Object> map = veOaYinzhangLogService.showTable(veOaYinzhangLogEntity,nameList,currentPage,pageSize);
        return Result.OK(map);
    }

    //删除单/多条记录
    @AutoLog(value = "公共印章使用记录-显示表格")
    @ApiOperation(value = "查看公章使用记录表格", notes = "公共印章使用记录-显示表格")
    @GetMapping(value = "/deleteAll")
    public Result<?> deleteAll(@RequestParam List<String> ids){
        System.out.println("数组"+ids);
        List<Long> list = new ArrayList<>();
        for (int i=0;i<ids.size();i++){
            System.out.println(ids.get(i));
            list.add(Long.parseLong(ids.get(i)));
        }
        int res = veOaYinzhangLogService.deleteYinzhangLog(list);
        if(res == 0){
            return Result.error(500,"删除失败");
        }
        return Result.OK("删除成功");
    }

    //导出excel表格
    @AutoLog(value = "公共印章使用记录-导出excel表格")
    @ApiOperation(value = "导出excel表格", notes = "公共印章使用记录-导出excel表格")
    @GetMapping(value = "/exportYinzhangLog")
    //ModelAndView 导出表格流
    public ModelAndView exportYinzhangLog(){
        //获取登录人信息
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        VeOaYinzhangLogEntity veOaYinzhangLogEntity = new VeOaYinzhangLogEntity();
        //查全公章使用记录
        veOaYinzhangLogEntity.setTypeStr("公共印章");
        List<VeOaYinzhangLogVo> voList = veOaYinzhangLogService.export(veOaYinzhangLogEntity);
        //创建导出流
        ModelAndView modelAndView = new ModelAndView(new JeecgEntityExcelView());
        //文件的文件名
        modelAndView.addObject(NormalExcelConstants.FILE_NAME,"公章使用记录表");
        //数据的类型
        modelAndView.addObject(NormalExcelConstants.CLASS, VeOaYinzhangLogVo.class);
        // ExportParams  Export工具类   （参数1--》title:表格标题  参数2--》secomdTitle:第二行的标题，  参数3--》secomdTitle:最下面尾部的信息）
        ExportParams exportParams = new ExportParams("公章使用记录表","导出人:"+user.getRealname(),"导出公章使用记录表");
        modelAndView.addObject(NormalExcelConstants.PARAMS,exportParams);
        modelAndView.addObject(NormalExcelConstants.DATA_LIST,voList);

        return modelAndView;
    }













}
